package com.imagination.applet.controller.api;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.alibaba.fastjson.JSONObject;
import com.imagination.applet.controller.api.request.SaveHeadUrlReq;
import com.imagination.applet.controller.api.response.QueryUserShareCodeResponse;
import com.imagination.applet.controller.web.configuration.UserHolder;
import com.imagination.applet.domain.user.service.UserService;
import com.imagination.applet.infrastructure.common.CommonProperties;
import com.imagination.applet.infrastructure.common.RestResponse;
import com.imagination.applet.infrastructure.utils.AccessTokenUtils;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Base64;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AccessTokenUtils accessTokenUtils;

    @Autowired
    private CommonProperties commonProperties;

    @GetMapping("/share-code")
//    @Operation(summary = "分享码")
    public RestResponse<QueryUserShareCodeResponse> shareCode() {
        Long userId = UserHolder.getUserId();
//        Long userId = 10000019l;
        byte[] getwxacodeunlimit = accessTokenUtils.getwxacodeunlimit(userId);
        String result = Base64.getEncoder().encodeToString(getwxacodeunlimit);
        return RestResponse.ok(QueryUserShareCodeResponse.builder()
                .shareCode(result)
                .build());
    }

    @GetMapping("/receiveciyuanbi")
//    @Operation(summary = "领取次元币")
    public RestResponse receiveciyuanbi(String source) {
        Long userId = UserHolder.getUserId();
        userService.receiveciyuanbi(userId,source);
        return RestResponse.ok();
    }


    @GetMapping("/getQrCode")
//    @Operation(summary = "分享码")
    public void getQrCodeLZJ(HttpServletResponse response,String source,String env) throws IOException {
//        Long userId = 10000019l;
//        byte[] getwxacodeunlimit = accessTokenUtils.getwxacodeunlimit("LZJ","pages/photoTemplate/index","trial");
//        byte[] getwxacodeunlimit = accessTokenUtils.getwxacodeunlimit("XHYKF","pages/photoTemplate/index","release");
        byte[] getwxacodeunlimit = accessTokenUtils.getwxacodeunlimit(source,"pages/photoTemplate/index",env);
        String result = Base64.getEncoder().encodeToString(getwxacodeunlimit);
        IoUtil.write(response.getOutputStream(),true,getwxacodeunlimit);
    }


    @GetMapping("/getFlag")
    public RestResponse<Boolean> getFlag(){
        return RestResponse.ok(commonProperties.getFlag());
    }

    @PostMapping("/save")
//    @Operation(summary = "用户头像")
    public RestResponse<Object> saveHeadUrl(@RequestBody SaveHeadUrlReq req) {
        userService.saveHeadUrl(req);
        return RestResponse.ok();
    }

    @PostMapping("/bindParent")
//    @Operation(summary = "绑定上级")
    public RestResponse<Object> bindParent(@RequestBody SaveHeadUrlReq req) {
        userService.bindParent(req.getParentId());
        return RestResponse.ok();
    }

    @PostMapping("/info")
//    @Operation(summary = "用户信息")
    public RestResponse<Object> info() {
        JSONObject data = userService.info();
        return RestResponse.ok(data);
    }
}
